//
// Created by pzw on 23-12-27.
//

#include "VpnConfig.h"
#include <iostream>
#include <fstream>
#include <string>

/**
 * 添加连接配置文件
 *
 * @param configFilePath 配置文件的路径
 */
void addConnectionConfig(const std::string &configFilePath) {
    std::ofstream configFile(configFilePath);
    if (configFile.is_open()) {
        std::string serverAddress, port, protocol;

        std::cout << "请输入服务器地址：";
        std::cin >> serverAddress;
        configFile << "remote " << serverAddress << std::endl;

        std::cout << "请输入端口号：";
        std::cin >> port;
        configFile << "port " << port << std::endl;

        std::cout << "请输入协议类型：";
        std::cin >> protocol;
        configFile << "proto " << protocol << std::endl;

        configFile.close();
        std::cout << "连接配置文件已添加：" << configFilePath << std::endl;
    } else {
        std::cerr << "无法打开连接配置文件：" << configFilePath << std::endl;
    }
}

/**
 * 编辑连接配置文件
 *
 * @param configFilePath 配置文件的路径
 */
void editConnectionConfig(const std::string &configFilePath) {
    std::ifstream configFile(configFilePath);
    if (configFile.is_open()) {
        std::string line;
        std::string newConfigContent;
        std::string serverAddress, port, protocol;

        while (std::getline(configFile, line)) {
            if (line.find("remote ") == 0) {
                std::cout << "当前服务器地址为：" << line.substr(7) << std::endl;
                std::cout << "请输入新的服务器地址：";
                std::cin >> serverAddress;
                line = "remote " + serverAddress;
            } else if (line.find("port ") == 0) {
                std::cout << "当前端口号为：" << line.substr(5) << std::endl;
                std::cout << "请输入新的端口号：";
                std::cin >> port;
                line = "port " + port;
            } else if (line.find("proto ") == 0) {
                std::cout << "当前协议类型为：" << line.substr(6) << std::endl;
                std::cout << "请输入新的协议类型：";
                std::cin >> protocol;
                line = "proto " + protocol;
            }
            newConfigContent += line + "\n";
        }

        configFile.close();

        std::ofstream updatedConfigFile(configFilePath);
        if (updatedConfigFile.is_open()) {
            updatedConfigFile << newConfigContent;
            updatedConfigFile.close();
            std::cout << "连接配置文件已编辑：" << configFilePath << std::endl;
        } else {
            std::cerr << "无法打开连接配置文件：" << configFilePath << std::endl;
        }
    } else {
        std::cerr << "无法打开连接配置文件：" << configFilePath << std::endl;
    }
}

/**
 * 删除连接配置文件
 *
 * @param configFilePath 配置文件的路径
 */
void deleteConnectionConfig(const std::string &configFilePath) {
    if (std::remove(configFilePath.c_str()) == 0) {
        std::cout << "连接配置文件已删除：" << configFilePath << std::endl;
    } else {
        std::cerr << "无法删除连接配置文件：" << configFilePath << std::endl;
    }
}


int vpn_config_main() {
    std::string configFilePath = "/path/to/config.ovpn";

    // 添加连接配置文件
    addConnectionConfig(configFilePath);

    // 编辑连接配置文件
    editConnectionConfig(configFilePath);

    // 删除连接配置文件
    deleteConnectionConfig(configFilePath);

    return 0;
}